package com.pervasive.geo_tag.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pervasive.geo_tag.server.Photo;

/**
 * Servlet implementation class ModifyServlet
 */
@WebServlet("/ModifyServlet")
public class ModifyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		

		Statement stmt = null;
		String dbName = "android_geo_tag";
		String queryPhoto;
		String queryNote;
		Connection con = null;
		String modNote = null;
		String id_photo = null;
	    ResultSet rs = null;
		Photo photo = new Photo();

		
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException e3) {
			e3.printStackTrace();
		} catch (IllegalAccessException e3) {
			e3.printStackTrace();
		} catch (ClassNotFoundException e3) {
			e3.printStackTrace();
		}
		try {
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/android_geo_tag", "root", "seppiessa");
		} catch (SQLException e1) {e1.printStackTrace();}
		try {
			stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
		} catch (SQLException e1) {e1.printStackTrace();}
		
		modNote = request.getHeader("note");
		id_photo = request.getHeader("id_photo");
		System.out.println(modNote);
		System.out.println(id_photo);
		
		queryPhoto = "select * from " + dbName + ".photo where id_photo = " + id_photo ;
		
		try {
			rs = stmt.executeQuery(queryPhoto);
		} catch (SQLException e1) {e1.printStackTrace();}
		
		try {
			while(rs.next()){
				photo.setId_photo(rs.getInt("ID_PHOTO"));
				photo.setUri_photo(rs.getString("URI"));
				photo.setData(rs.getString("DATA"));
				photo.setSize(rs.getLong("SIZE"));
				photo.setId_note(rs.getInt("ID_NOTE"));
				photo.setLocation(rs.getString("LOCATION"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		queryNote = "update " + dbName + ".note set text='"+ modNote + "' where id_note = " + photo.getId_note();
		
		try {
			stmt.executeUpdate(queryNote);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}

}
